package com.csgsystems.im.survey.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.csgsystems.im.struts2.action.BaseAjaxActionSupport;
import com.csgsystems.im.survey.Language;
import com.csgsystems.im.survey.Question;
import com.csgsystems.im.survey.QuestionType;
import com.csgsystems.im.survey.Survey;
import com.csgsystems.im.survey.SurveyStatus;
import com.csgsystems.im.survey.persistence.LanguageDao;
import com.csgsystems.im.survey.persistence.QuestionDao;
import com.csgsystems.im.survey.persistence.QuestionTypeDao;
import com.csgsystems.im.survey.persistence.SurveyDao;

public class PrototypeAction extends BaseAjaxActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5101779479417265696L;
	
	SurveyDao surveyDao = null;
	QuestionDao questionDao = null;
	QuestionTypeDao questionTypeDao = null;
	LanguageDao languageDao = null;
	
	private long surveyId = 0;
	private List<Survey> survey = null;
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
		
	}
	
	public String questions() {
		List<Question> results = null;
		if ((results = (List<Question>) getSession().getAttribute("questionResults")) != null) {
			Map<String, Object> responseMap = new HashMap<String, Object>();
			responseMap.put("questionStore", results);
			responseMap.put("totalCount", results.size());
			setResponseData(responseMap);
			
		} else if ((results = questionDao.findBySurveyId(surveyId)) == null) {
			
		} else {
			getSession().setAttribute("questionResults", results);
			Map<String, Object> responseMap = new HashMap<String, Object>();
			responseMap.put("questionStore", results);
			responseMap.put("totalCount", results.size());
			setResponseData(responseMap);
			
		}
		return JSON_RESULT; 
	}
	
	public String questionTypes() {
		List<QuestionType> results = null;
		if ((results = (List<QuestionType>) getSession().getAttribute("questionTypeResults")) != null) {
			Map<String, Object> responseMap = new HashMap<String, Object>();
			responseMap.put("questionTypeStore", results);
			responseMap.put("totalCount", results.size());
			setResponseData(responseMap);
			
		} else if ((results = questionTypeDao.findAll()) == null) {
			
		} else {
			getSession().setAttribute("questionTypeResults", results);
			Map<String, Object> responseMap = new HashMap<String, Object>();
			responseMap.put("questionTypeStore", results);
			responseMap.put("totalCount", results.size());
			setResponseData(responseMap);
			
		}
		return JSON_RESULT; 
	}
	
	public String languages() {
		List<Language> results = null;
		if ((results = (List<Language>) getSession().getAttribute("languageResults")) != null) {
			Map<String, Object> responseMap = new HashMap<String, Object>();
			responseMap.put("languageStore", results);
			responseMap.put("totalCount", results.size());
			setResponseData(responseMap);
			
		} else if ((results = languageDao.findAll()) == null) {
			
		} else {
			getSession().setAttribute("languageResults", results);
			Map<String, Object> responseMap = new HashMap<String, Object>();
			responseMap.put("languageStore", results);
			responseMap.put("totalCount", results.size());
			setResponseData(responseMap);
			
		}
		return JSON_RESULT; 
	}
	
	public String surveyStatuses() {
		Map<String, Object> responseMap = new HashMap<String, Object>();
		responseMap.put("surveyStatusStore", Arrays.asList(SurveyStatus.values()));
		responseMap.put("totalCount", SurveyStatus.values().length);
		setResponseData(responseMap);
		return JSON_RESULT; 	
	}
	
	public String list() {
		
		List<Survey> results = null;
		if ((results = (List<Survey>) getSession().getAttribute("surveyResults")) != null) {
			Map<String, Object> responseMap = new HashMap<String, Object>();
			responseMap.put("surveyStore", results);
			responseMap.put("totalCount", results.size());
			setResponseData(responseMap);
			
		} else if ((results = surveyDao.findByHierarchyItem(847)) == null) {
			
		} else {
			getSession().setAttribute("surveyResults", results);
			Map<String, Object> responseMap = new HashMap<String, Object>();
			responseMap.put("surveyStore", results);
			responseMap.put("totalCount", results.size());
			setResponseData(responseMap);
			
		}
		return JSON_RESULT; 
		
	}

	public String add() {
		List<Survey> results = new ArrayList<Survey>();
		Map<String, Object> responseMap = new HashMap<String, Object>();
		try {
			survey.set(0, 
				surveyDao.create(survey.get(0)));
			results.add(survey.get(0));
			getSession().removeAttribute("surveyResults");
			
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.getMessage());
			responseMap.put("message", e.getMessage());
			
		}
		responseMap.put("surveyStore", results);
		setResponseData(responseMap);
		return JSON_RESULT;
		
	}
	
	public String update() {
		List<Survey> results = new ArrayList<Survey>();
		Map<String, Object> responseMap = new HashMap<String, Object>();
		try {
			survey.set(0,  
				surveyDao.update(survey.get(0).getId(), survey.get(0)));
			results.add(survey.get(0));
			getSession().removeAttribute("surveyResults");
			
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.getMessage());
			responseMap.put("message", e.getMessage());
			
		}
		responseMap.put("surveyStore", results);
		setResponseData(responseMap);
		return JSON_RESULT;
		
	}
	
	public String delete() {
		List<Survey> results = new ArrayList<Survey>();
		Map<String, Object> responseMap = new HashMap<String, Object>();
//		try {
//			configService.deleteDidReportingConfig(didReportingConfig.get(0).getDigits());
//			results.add(didReportingConfig.get(0));
//		} catch (Exception e) {
//			e.printStackTrace();
//			addActionError(e.getMessage());
//			responseMap.put("message", e.getMessage());
//			
//		}
		responseMap.put("surveyStore", results);
		setResponseData(responseMap);
		return JSON_RESULT;
		
	}

	public long getSurveyId() {
		return surveyId;
	}

	public void setSurveyId(long surveyId) {
		this.surveyId = surveyId;
	}

	public List<Survey> getSurvey() {
		return survey;
	}

	public void setSurvey(List<Survey> survey) {
		this.survey = survey;
	}

	public void setSurveyDao(SurveyDao surveyDao) {
		this.surveyDao = surveyDao;
	}

	public void setQuestionDao(QuestionDao questionDao) {
		this.questionDao = questionDao;
	}

	public void setQuestionTypeDao(QuestionTypeDao questionTypeDao) {
		this.questionTypeDao = questionTypeDao;
	}

	public void setLanguageDao(LanguageDao languageDao) {
		this.languageDao = languageDao;
	}

}
